﻿using System.IO;
using System.Text;
using ResxHelper.Object;
using System;
using System.Windows.Input;
using ResxHelper.Logic.Formatters;

namespace ResxHelper.Logic.Actions
{
    class SaveCsvAction : ICommand
    {
        private readonly ResourceFile _input;
        private readonly string _savePath;

        public SaveCsvAction(ResourceFile file, string savePath)
        {
            _input = file;
            _savePath = savePath;
        }

        public bool CanExecute(object parameter)
        {
            return
                _input != null &&
                !string.IsNullOrEmpty(_savePath);
        }

        public event EventHandler CanExecuteChanged;

        public void Execute(object parameter)
        {
            var csv = ResourceFileFormatterFactory.GetFormatter(_input, SaveFileType.CsvString).Run();
            using (var writer = new StreamWriter(_savePath, false, Encoding.UTF8))
            {
                writer.Write(csv);
            }
        }
    }
}
